package view.room;

import common.RoomState;
import model.room.Room;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
/*
 * Created by JFormDesigner on Sat May 25 00:05:02 CST 2024
 */


/**
 * @author 孟晗
 */
public class RoomFrame extends JPanel {
    private RoomTableModel roomTableModel;
    public RoomFrame() {
        this.roomTableModel = roomTableModel != null ? roomTableModel : new RoomTableModel();
        String filePath = "rooms.csv"; // CSV文件路径
        Room.setRooms(Room.getRoomInformation());
        initComponents();
        table1.setModel(roomTableModel);
    }



    private void button1(ActionEvent e) {
        AddRoom addRoom=new AddRoom(roomTableModel);
        addRoom.setVisible(true);
    }





    private void button3(ActionEvent e) {
        // TODO add your code here
        int rowIndex = table1.getSelectedRow();
        if(rowIndex==-1)
            JOptionPane.showMessageDialog(this,"请选择要删除的行");
        else {
            int result = JOptionPane.showConfirmDialog(this,"是否删除该房间");
            if(result==JOptionPane.YES_OPTION) {
                RoomTableModel model = (RoomTableModel)table1.getModel();
                String id=(String)table1.getValueAt(rowIndex,0);
                model.deleteRoom(id);
            }
            else
                JOptionPane.showMessageDialog(null,"已取消删除");
            ((RoomTableModel)table1.getModel()).fireTableDataChanged();



        }

    }



    private void button2(ActionEvent e) {

            int rowIndex = table1.getSelectedRow();
            if (rowIndex == -1) {
                JOptionPane.showMessageDialog(this, "请选择要修改的行");
            } else {
                RoomTableModel model = (RoomTableModel) table1.getModel();
                String id = (String) table1.getValueAt(rowIndex, 0);

                // 显示输入对话框，让用户输入新的房间状态
                String userInput = JOptionPane.showInputDialog(this, "请输入新的房间状态（BOOKED/EMPTY/CHECKIN/REPAIR）");
                if (userInput != null && userInput.equalsIgnoreCase("BOOKED")) {
                    model.updateRoom(id, RoomState.BOOKED);
                } else if (userInput != null && userInput.equalsIgnoreCase("EMPTY")) {
                    model.updateRoom(id, RoomState.EMPTY);
                } else if(userInput != null && userInput.equalsIgnoreCase("CHECKIN")) {
                    model.updateRoom(id, RoomState. CHECKIN);
                }else if(userInput != null && userInput.equalsIgnoreCase("REPAIR")) {
                    model.updateRoom(id, RoomState.REPAIR);
                } else {
                    JOptionPane.showMessageDialog(this, "无效的房间状态输入，请输入BOOKED/EMPTY/CHECKIN/REPAIR");
                }

                // 更新表格数据
                ((RoomTableModel) table1.getModel()).fireTableDataChanged();
            }
        }








    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - mh
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setPreferredSize(new Dimension(515, 500));
        setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(new javax.swing.
        border.EmptyBorder(0,0,0,0), "JF\u006frm\u0044es\u0069gn\u0065r \u0045va\u006cua\u0074io\u006e",javax.swing.border.TitledBorder.CENTER
        ,javax.swing.border.TitledBorder.BOTTOM,new Font("D\u0069al\u006fg", Font
        .BOLD,12), Color.red), getBorder())); addPropertyChangeListener(
        new java.beans.PropertyChangeListener(){@Override public void propertyChange(java.beans.PropertyChangeEvent e){if("\u0062or\u0064er"
        .equals(e.getPropertyName()))throw new RuntimeException();}});

        //---- button1 ----
        button1.setText("\u6dfb\u52a0\u623f\u95f4");
        button1.addActionListener(e -> button1(e));

        //---- button2 ----
        button2.setText("\u7f16\u8f91\u623f\u95f4");
        button2.addActionListener(e -> button2(e));

        //---- button3 ----
        button3.setText("\u5220\u9664\u623f\u95f4");
        button3.addActionListener(e -> button3(e));

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(table1);
        }

        GroupLayout layout = new GroupLayout(this);
        setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup()
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup()
                        .addGroup(layout.createSequentialGroup()
                            .addGap(40, 40, 40)
                            .addComponent(button1)
                            .addGap(53, 53, 53)
                            .addComponent(button2)
                            .addGap(64, 64, 64)
                            .addComponent(button3))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(15, 15, 15)
                            .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 478, GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup()
                .addGroup(layout.createSequentialGroup()
                    .addGap(35, 35, 35)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(button1)
                        .addComponent(button2)
                        .addComponent(button3))
                    .addGap(18, 18, 18)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 285, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(17, Short.MAX_VALUE))
        );
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - mh
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on


}
